package com.szewec.ps.myview.videoplay;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.VideoView;

import com.szewec.ps.R;
import com.szewec.ps.util.LogUtil;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

public class VideoPlayActivity extends BaseActivity {

    private String url;
    private String TAG = "VideoPlayActivity";
    private CustomJCVideoPlayerStandard videoplayer;
    private VideoView videoview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_play);
        getdata();

//        //网络视频
//
//        videoview = (VideoView) this.findViewById(R.id.videoView);
//
//        //设置视频控制器
//        videoview.setMediaController(new MediaController(this));
//
//        //播放完成回调
//        videoview.setOnCompletionListener(new MyPlayerOnCompletionListener());
//
//        //设置视频路径
////        videoView.setVideoURI(uri);
////        videoview.setVideoPath("http://video.gcnao.cn/video/21bb3ab8-167cf0c39fe-0000-0000-013-eec4b.mp4");
//        videoview.setVideoPath(url);
//
//        //开始播放视频
//        videoview.start();


        CustomJCVideoPlayerStandard.FULLSCREEN_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;  //横向
        CustomJCVideoPlayerStandard.NORMAL_ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT;  //纵向

        videoplayer = (CustomJCVideoPlayerStandard)this.findViewById(R.id.videoplayer);


        boolean up = videoplayer.setUp(url, JCVideoPlayer.SCREEN_LAYOUT_LIST, "");
        if (up) {
            //展示播放器图片并充满
            videoplayer.thumbImageView.setScaleType(ImageView.ScaleType.FIT_XY);
//            Glide.with(this).load(bean.feed).into(videoplayer.thumbImageView);
        }
        int duration = videoplayer.getDuration();
        videoplayer.startPlayLogic();

        //在setUp方法之后设置
        videoplayer.backButton.setVisibility(View.VISIBLE);
        videoplayer.backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void getdata() {
        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        LogUtil.i(TAG,"url:"+url);
    }


    @Override
    public void onBackPressed() {
//        "全屏竖屏切换的时候继续播放"
        if (CustomJCVideoPlayerStandard.backPress()){
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
//        "释放资源停止视频播放"
        CustomJCVideoPlayerStandard.releaseAllVideos();

    }
    class MyPlayerOnCompletionListener implements MediaPlayer.OnCompletionListener {

        @Override
        public void onCompletion(MediaPlayer mp) {
            Toast.makeText(VideoPlayActivity.this, "播放完成了", Toast.LENGTH_SHORT).show();
        }

    }
}
